package org.ls.petclinic.web;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.ANEWARRAY;
import org.ls.petclinic.dao.OwnerDao;
import org.ls.petclinic.dao.impl.OwnerDaoImpl;
import org.ls.petclinic.pojo.Owner;
import org.ls.petclinic.untils.JdbcUtils;
import org.ls.petclinic.untils.WebUtils;

import javax.jws.Oneway;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 浪矢先生
 * @description
 * @create 2022-12-05 22:03
 */
@WebServlet(urlPatterns = "/getOwner")
public class GetOwner extends BaseServlet {
    private OwnerDao ownerDao = new OwnerDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Gson gson = new Gson();
        List<Owner> result = new ArrayList<>();
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        String name = req.getParameter("name");
        String id1 = req.getParameter("id");
        Long id=null;
        if (!ObjectUtil.isNull(id1)){
            id = Long.parseLong(id1);
        }
        if (!ObjectUtil.isNull(name)) {
            result = ownerDao.getByName(name);
        } else if (!ObjectUtil.isNull(id)) {
            result.add(ownerDao.getById(id));
        } else {
            result = ownerDao.getAll();
        }
        JdbcUtils.commitAndClose();
        resp.getWriter().write(String.valueOf(gson.toJson(result)));
    }
}
